package com.itcam.web.controller.chat;


import com.itcam.chat.domain.ChatKnowledge;
import com.itcam.chat.service.IChatKnowledgeService;
import com.itcam.common.annotation.Log;
import com.itcam.common.core.controller.BaseController;
import com.itcam.common.core.domain.AjaxResult;
import com.itcam.common.core.page.TableDataInfo;
import com.itcam.common.enums.BusinessType;
import com.itcam.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 知识库管理Controller
 *
 * @author cammy
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/chat/knowledge")
public class ChatKnowledgeController extends BaseController {
    @Autowired
    private IChatKnowledgeService chatKnowledgeService;

    /**
     * 查询知识库管理列表
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatKnowledge chatKnowledge) {
        startPage();
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        return getDataTable(list);
    }

    /**
     * 导出知识库管理列表
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:export')")
    @Log(title = "知识库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatKnowledge chatKnowledge) {
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        ExcelUtil<ChatKnowledge> util = new ExcelUtil<ChatKnowledge>(ChatKnowledge.class);
        util.exportExcel(response, list, "知识库管理数据");
    }

    /**
     * 获取知识库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:query')")
    @GetMapping(value = "/{knowledgeId}")
    public AjaxResult getInfo(@PathVariable("knowledgeId") Long knowledgeId) {
        return success(chatKnowledgeService.selectChatKnowledgeByKnowledgeId(knowledgeId));
    }

    /**
     * 新增知识库管理
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:add')")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatKnowledge chatKnowledge) {
        return toAjax(chatKnowledgeService.insertChatKnowledge(chatKnowledge));
    }

    /**
     * 修改知识库管理
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:edit')")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatKnowledge chatKnowledge) {
        return toAjax(chatKnowledgeService.updateChatKnowledge(chatKnowledge));
    }

    /**
     * 删除知识库管理
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:remove')")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{knowledgeIds}")
    public AjaxResult remove(@PathVariable Long[] knowledgeIds) {
        return toAjax(chatKnowledgeService.deleteChatKnowledgeByKnowledgeIds(knowledgeIds));
    }

    // 前端点击上传知识库根据项目id查询知识库列表
    // http://localhost/dev-api/chat/knowledge?projectId=114

    /**
     * 点击上传知识库 根据当前project_id获取知识库列表
     * @param chatKnowledge
     * @return
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('chat:knowledge:list')")
    public TableDataInfo getKnowledgeListByProjectId(ChatKnowledge chatKnowledge) {
        startPage();
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        return getDataTable(list);
    }

    /**
     * 知识库文件上传
     * @param chatKnowledge
     * @param file
     * @return
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:add')")
    @PostMapping("upload")
    public AjaxResult upload(ChatKnowledge chatKnowledge, MultipartFile file) {
        chatKnowledge.setUserId(getUserId());
        chatKnowledge.setCreateBy(getUsername());
        Long result = chatKnowledgeService.upload(chatKnowledge, file);
        return success(result);
    }

    /**
     * 知识库文件删除
     * @param knowledge
     * @return
     */
    @PreAuthorize("@ss.hasPermi('chat:knowledge:remove')")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody ChatKnowledge knowledge) {
        return toAjax(this.chatKnowledgeService.remove(knowledge.getProjectId(), knowledge.getKnowledgeId()));
    }
}
